package com.ruoyi.web.controller.clothing;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.clothing.domain.User;
import com.ruoyi.clothing.domain.vo.AddAddress;
import com.ruoyi.clothing.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.clothing.domain.SysAddress;
import com.ruoyi.clothing.service.ISysAddressService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地址管理Controller
 *
 * @author weh
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/clothing/address")
public class SysAddressController extends BaseController
{
    @Autowired
    private ISysAddressService sysAddressService;

    @Autowired
    private IUserService userService;

    /**
     * 查询地址管理列表
     */
//    @PreAuthorize("@ss.hasPermi('clothing:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAddress sysAddress)
    {
        startPage();
        List<SysAddress> list = sysAddressService.selectSysAddressList(sysAddress);
        return getDataTable(list);
    }

    /**
     * 导出地址管理列表
     */
//    @PreAuthorize("@ss.hasPermi('clothing:address:export')")
    @Log(title = "地址管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAddress sysAddress)
    {
        List<SysAddress> list = sysAddressService.selectSysAddressList(sysAddress);
        ExcelUtil<SysAddress> util = new ExcelUtil<SysAddress>(SysAddress.class);
        util.exportExcel(response, list, "地址管理数据");
    }

    /**
     * 获取地址管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('clothing:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(sysAddressService.selectSysAddressById(id));
    }

    /**
     * 新增地址管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:address:add')")
    @Log(title = "地址管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAddress sysAddress)
    {
        return toAjax(sysAddressService.insertSysAddress(sysAddress));
    }

    /**
     * 修改地址管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:address:edit')")
    @Log(title = "地址管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAddress sysAddress)
    {
        return toAjax(sysAddressService.updateSysAddress(sysAddress));
    }

    /**
     * 删除地址管理
     */
//    @PreAuthorize("@ss.hasPermi('clothing:address:remove')")
    @Log(title = "地址管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysAddressService.deleteSysAddressByIds(ids));
    }

    /**
     * 根据用户名获取用户的地址列表
     * @param username 用户名
     * @return 地址列表
     */
    @GetMapping("/selectUserByUserName")
    public List<SysAddress> selectAddressListByUsername(@RequestParam String username){
        User user = userService.selectUserByUserName(username);
        Long id = user.getId();
        return sysAddressService.selectAddressListById(id);
    }

    /**
     * 根据id获取用户的地址列表
     * @param
     * @return
     */
    @GetMapping("/selectUserAddressById/{addressOwner}")
    public List<SysAddress> selectUserAddressById(@PathVariable Long addressOwner){
        return sysAddressService.selectAddressListById(addressOwner);
    }

    /**
     * 新增地址
     * @param data
     * @return
     */
    @PostMapping("/insertAddress")
    public AjaxResult insertAddress(@RequestBody AddAddress data){
        String username = data.getUsername();
        User user = userService.selectUserByUserName(username);
        Long id = user.getId();
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",data.getName());
        map.put("tel",data.getTel());
        map.put("address",data.getAddress());
        map.put("d_address",data.getdAddress());
        map.put("address_owner",id);
        System.out.println("map = " + map);
        sysAddressService.insertSysAddressWithID(map);
        return AjaxResult.success("新增成功");
    }


    @DeleteMapping("/deleteAddressById")
    public AjaxResult deleteAddressById(@RequestParam Integer id){
        sysAddressService.deleteAddressById(id);
        return AjaxResult.success("删除成功");
    }
}
